define(window.backboneCommon.getViewDefine("user.index"), function ($,require,Collection,tpl,ViewDetail) {
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_AddUser": "addUser",
            "click .edit-user": "editUser"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.render();
        },
        render: function () {
            var templateHtml = _.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain: function () {
            this.elementModel = {};
            this.extObject = {};
            this.pageData = {};
            this.elementModel.table = $("#tb_listUser");
            this.initViewAttach();
            this._initInputSearch();
            this._initBodyMain();
        },
        _initBodyMain: function () {
            this._initPageList();
        },
        initViewAttach:function () {
            var self=this;
            self.viewAttach={};
            self.viewAttach.detail=new ViewDetail({pElement:"#container_UserDetail",
                submitCallback:function (isAdd,info) {
                    if(isAdd){
                        self.refreshList();
                    }else{
                        var tr=$("#userid_"+info.id).parent();
                        var row = $("#tb_listUser").ExtDataTable().getRowInfo(tr);
                        row.data()["status"]=info.status;
                        self._bindTableStatus(tr,info.status)
                    }
                }
            });
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"用户ID,用户名称",
                searchCallback:function () {
                    self.extObject.table.refresh()
                }
            })
        },
        _initPageList: function () {
            var self = this;
            var options = {
                columns: [
                    {data: "id", class: "w-60"},
                    {data: "name", class: "w-min-100"},
                    {data: "roleName", class: "w-300"},
                    {data: "status", class: "w-80"}
                ],
                infoExt: {
                    ajaxDataPara: function () {
                        var param = {};
                        var keyword = self.extObject.inputSearch.getValue();
                        if (!$.ExtCommon.isCheckEmpty(keyword)) {
                            param.search = keyword;
                        }
                        return param;
                    },
                    ajaxWholeCallback: function (param) {
                        self.collection.getPageList(param)
                    }
                },
                createdRow: function (row, data) {
                    var tds=$('td', row);
                    tds.eq(1).attr("id", "userid_" + data.id).html('<a class="btn-operate longterm edit-user" href="javascript:">' + data.name + '</a>');
                    var isCustomer=data.userRole==="advertiser";
                    tds.eq(2).html(isCustomer?(data.roleName+"-"+data.customerName):data.roleName);
                    self._bindTableStatus(row,data.status);
                },
                drawCallback: function () {
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            self.extObject.table = self.elementModel.table.ExtDataTable(options);
        },
        _bindTableStatus:function(tr,status){
            var isStart=status.toString() === "1";
            $('td', tr).eq(3).html($.ExtCommon.getStringConnect('<div class="{0}" >{1}</div>', [isStart? "text-green" : "text-red", isStart ? "有效" : "暂停中"]));
        },
        bindList: function () {
            var data = this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()},
        addUser:function () {
                this.viewAttach.detail.init({isAdd:true,status:"1",useRole:"agent"});
        },
        editUser:function (e) {
            var tr = $(e.currentTarget).parents("tr:first");
            var rowData = this.extObject.table.getRowInfo(tr).data();
            rowData.isAdd=false;
            this.viewAttach.detail.init(rowData)
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});